package ubi.quando.UI;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import ubi.quando.App.Contact;
import ubi.quando.App.EventBase;
import ubi.quando.App.EventMeeting;
import ubi.quando.App.PickUpParticipant;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class AddParticipantsMeetingActivity extends AddParticipantsBaseActivity {

	private Set<Contact> mAdded;
	private Set<Contact> mRemoved;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mAdded = new HashSet<Contact>();
		mRemoved = new HashSet<Contact>();

		mTableView.setVisibility(View.GONE);

		InvalidateList();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mApp.setCurrentActivity(this);
		
		Bundle b = getIntent().getExtras();
		if (b != null) {
			mEvent = (EventMeeting) b.getSerializable(EventBase.SerializeName);
		}

		Map<Contact, PickUpParticipant> awaiting = mEvent.getAwaiting();
		Iterator<Map.Entry<Contact, PickUpParticipant>> iter = awaiting
				.entrySet().iterator();
		while (iter.hasNext()) {
			Contact next = iter.next().getKey();
			mAdded.add(next);
		}
		mRemoved.clear();

		InvalidateList();
	}

	@Override
	protected void InitListeners() {
		mTextView.setOnItemClickListener(new ACOnItemClickListener());
		mListView.setOnItemClickListener(new LVOnItemClickListener());
		mOkBtn.setOnClickListener(new OkOnClickListener());
		mCancelBtn.setOnClickListener(new CancelOnClickListener());
	}

	private void InvalidateList() {
		mListView.setAdapter(new ArrayAdapter<Contact>(this,
				R.layout.lv_list_item, mAdded.toArray(new Contact[mAdded
						.size()])));
		mListView.invalidate();
	}

	public class ACOnItemClickListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int pos,
				long id) {
			Contact contact = (Contact)parent.getItemAtPosition(pos);
			if (!mAdded.contains(contact)) {
				mAdded.add(contact);
				mRemoved.remove(contact);
				InvalidateList();
			} else {
				Toast.makeText(getApplicationContext(),
						"This participant is already in the list",
						Toast.LENGTH_LONG).show();
			}

			mTextView.setText("");
		}
	}

	public class LVOnItemClickListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int pos,
				long id) {
			Contact contact = (Contact) parent.getItemAtPosition(pos);
			mAdded.remove(contact);
			mRemoved.add(contact);
			InvalidateList();
		}
	}

	public class OkOnClickListener implements OnClickListener {

		public void onClick(View v) {
			for (Contact c : mAdded) {
				mEvent.AddInvited(c, null);
			}
			for (Contact c : mRemoved) {
				mEvent.RemoveInvited(c);
			}

			Bundle b = new Bundle();
			b.putSerializable(EventBase.SerializeName, mEvent);
			Intent intent = new Intent(v.getContext(),
					ArrangeMeetingActivity.class);
			intent.putExtras(b);
			startActivity(intent);
		}

	}

	public class CancelOnClickListener implements OnClickListener {

		public void onClick(View v) {
			Bundle b = new Bundle();
			b.putSerializable(EventBase.SerializeName, mEvent);
			Intent intent = new Intent(v.getContext(),
					ArrangeMeetingActivity.class);
			intent.putExtras(b);
			startActivity(intent);
		}

	}

}
